//
//  FileUtil.m
//  ESignature_HD
//
//  Created by SimonDing on 14-9-22.
//  Copyright (c) 2014年 china-sss. All rights reserved.
//

#import "FileUtil.h"

@implementation FileUtil

#pragma mark -获取应用程序的根目录
+ (NSString *)getAppBasePath
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = [pathArray objectAtIndex:0];
    return basePath;
}

#pragma mark -检测文件是否存在
+ (BOOL)isFileExist:(NSString *)filePath {
    if (((NSNull *)filePath == [NSNull null]) || filePath.length == 0) {
        return NO;
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    return result;
}

#pragma mark -根据文件路径获取文件名称
+ (NSString *)getFileNameByPath:(NSString *)filePath {
    NSString *fileName = @"";
    if (filePath.length == 0) {
        return fileName;
    }
    fileName = [filePath lastPathComponent];
    return fileName;
}

#pragma mark -删除指定路径的文件
+ (BOOL)removeFileAtPath:(NSString *)filePath
{
    BOOL isRemoved = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:filePath error:nil];
    return isRemoved;
}


/*文件是否存在*/
+ (BOOL)isFileExisted:(NSString *)fileName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:[self getFilePath:fileName]]) {
        return NO;
    }
    return YES;
}

/*创建指定名字的文件*/
+ (BOOL)createFileAtPath:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [array[0] stringByAppendingPathComponent:fileName];
    NSLog(@"-----%@:", path);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path]) {
        [fileManager createFileAtPath:path contents:nil attributes:nil];
        return YES;
    }
    return NO;
}

/*创建指定名字的文件夹*/
+ (BOOL)createDirectoryAtPath:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [array[0] stringByAppendingPathComponent:fileName];
    NSLog(@"-----%@:", path);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path]) {
        NSError *error = nil;
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        return YES;
    }
    return NO;
}

/*得到文件路径*/
+ (NSString *)getFilePath:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [array[0] stringByAppendingPathComponent:fileName];
    
    return path;
}

/*删除文件*/
+ (BOOL)deleteFileAtPath:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [array[0] stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:path]) {
        return NO;
    }
    [fileManager removeItemAtPath:path error:nil];
    return YES;
}




@end
